Faster algorithm.