Added test and refactoring