Generalized the stack framework.

Now I can use it to keep track of orphans in addition to clearing the tree.
Possibly fixes #43.