Refactored solution to problem #5 section 6.1 solution