Improve tree traversal.  Fixes #2