use queue to avoid recursion