Changed to use priority queue