implemented priority queue version