Fix random crashes in W-tiling code
W-tiling occasionally causes a random crash. This turned out to be caused by an attempt to read and pop an element from an empty queue.
This MR fixes that problem by adding a check for non-emptiness of the queue before trying to access its elements.