improve the fitting function