write fit function