More refactoring and more unit tests