slightly redefined the fitting function