added least squares fitting