cleaned up my maths nonsense, with comments (no functionality change)