updated refactored solution