minor refactoring and tests