Refactored the Solution