completed solution and refactored solution