Refactor the code using one queue.