Improve solution to #2.