refactoring and test fixes