adding tests and refactoring