More refactoring, adding unit tests as well