working priority queue, cleaned up code