Refactored fit_polynomial to return proper w