more refactoring and tests