Use heapq instead of queue