Updated with refactored solution