Fixes some typos, outdated comments and test asserts. Also uses safe math and `defensive` for arithmetic operations.