Refactored the code and unit test added.