Substantial improvements to math lib