Added some error checking to math and added tests.