improved speed in problem 10