Refactored solution 3