Changed from queue to deque due to problem with emptying queue.