first version with priority queue