Refactored tree handler