One possibly easy fix or at least workaround could be collecting documents to perform actions on, and only *then* perform the actions. So walking the tree is disconnected from anything that might alter it. This is kind of a workaround, but it seems a lot easier than coupling the tree relayout and tree walking in this case.