Improve algorithms for queues