Use quadratic err functions

and some clean up