Tree traversal - improving performance