fixing tests and refactoring