improved performance of solution to problem 32