Improve solution to problem #47