adding tests and refactor