more refactorings and tests