smart queue to ensure correct traversal order