refactored tree traversing functions