More refactorings and tests