Refactored the code