Re-write the code for polynomial function fitting