More progress on unit tests and refactoring