refactored solution 31