Improved unit tests and test coverage. Some slight refactoring.