Refactoring and adding test case