this is rewrite of the github patch #674 by choicely

the goal is to improve polyfit accuracy