Junior doctors agree to contract