Improves running time for problem 23