improved performance of solution to problem 34