refactored and added a test case