refactored, added tree->flat comparisons