Added more tests and refactored some code.