improoved priority queue code