refactored solution