Making solution to problem 39 more concise