Cleanup, Refactor

Refactored some code and tests and cleaned up a few other things.