Refactored the Code