Rewrote the tree event handling to fix a bug found in QA.