More refactoring and testing