refactoring and more unit tests