More refactoring, and first tests