improved performance of solution to problem 35