Implemented least squares method