Various bug fixes to math and asserting inputs before attempting calculation.