Updated unit tests, refactored some code