refactored solution based on feedback