Added more unit tests and refactoring