fixed bugs in new fitting function