improved performance of solution to problem 25