code refactored and test cases added.