some more refactoring and tests