Refactored solution for phase 1