fixed problem with nans on the last stage of model refinement