Refactored solution