Added some checks for user errors.
Removed some error-checks that can't happen.
Added a bunch of test cases.
Reorganized test cases to be after each function, rather than all in one place.