working on fitting function