Refactoring + added test cases