Refactor the priority queue