added tree balancing