refactoring and tests