Refactor tree traversal.