refactored the solution