Changed to using a priority queue.