We need to re-work the data structure for the tree. It was made to be recursive because that's how syntax trees logically operate, but that doesn't mean that we have to store data like that.
Think through creating a non-recursive structure that we can loop through and track the indices of children/parents as properties and add helper methods to pluck those out of the original tree.
Minimally this removes adding/removing the scope detail all at once. We could also think through how to minify the data structure as well which will directly reduce memory usage and how fast we can add/restore from cache and send around.