 	}
 	
 	func updateTree(replace parent: GeoContinent, with children: Set<GeoCountry>) {
-		worldTree.remove(value: parent.hashValue)
+		worldTree.remove(hashValue: parent.hashValue)
 		for child in children {
-			worldTree.insert(value: child.hashValue, region: child.geometry.aabb)
+			let countryBox = RegionBounds(regionHash: child.hashValue, bounds: child.geometry.aabb)
+			worldTree.insert(value: countryBox, region: countryBox.bounds)
 		}
 	}
 
 	func updateTree(replace parent: GeoCountry, with children: Set<GeoRegion>) {
-		worldTree.remove(value: parent.hashValue)
+		worldTree.remove(hashValue: parent.hashValue)
 		for child in children {
-			worldTree.insert(value: child.hashValue, region: child.geometry.aabb)
+			let regionBox = RegionBounds(regionHash: child.hashValue, bounds: child.geometry.aabb)
+			worldTree.insert(value: regionBox, region: regionBox.bounds)
 		}
 	}
 

}

// MARK: Debug rendering
func debugRenderTree(_ tree: QuadTree, at focus: Aabb) {
	debugQuadNode(tree.root, at: focus)
	_ = DebugRenderer.shared.addTransientQuad(for: focus, alpha: 1.0, name: "Focus", color: .red)
}

func debugQuadNode(_ node: QuadNode, at focus: Aabb) {
	if case let .Node(bounds, values, tl, tr, bl, br) = node {
		let highlight =  !( focus.minX >= bounds.maxX ||
												focus.maxX <= bounds.minX ||

import Foundation

class UIState {
	struct RegionBounds: Hashable {
		let regionHash: Int
		let bounds: Aabb
		func hash(into hasher: inout Hasher) {
			hasher.combine(regionHash)
		}
	}
	
	private var selectedRegionHash: Int = 0
	var worldTree: QuadTree<RegionBounds>!
	
	func cullWorldTree(focus: Aabb) -> Set<Int> {
		func intersects(_ a: Aabb, _ b: Aabb) -> Bool {
			return !( a.minX >= b.maxX ||
								a.maxX <= b.minX ||
								a.minY >= b.maxY ||
								a.maxY <= b.minY)
		}
		
		let roughlyVisible = worldTree.query(search: focus)
		let finelyVisible = roughlyVisible.filter { intersects($0.bounds, focus) }
		let visibleHashes = Set(finelyVisible.map { $0.regionHash })
		return visibleHashes
	}
	
	func buildWorldTree(withWorld geoWorld: GeoWorld, userState: UserState) {
		worldTree = QuadTree(minX: -180.0, minY: -90.0, maxX: 181.0, maxY: 90.0, maxDepth: 6)
		for continent in geoWorld.children {
			let continentBox = RegionBounds(regionHash: continent.hashValue, bounds: continent.geometry.aabb)
			worldTree.insert(value: continentBox, region: continentBox.bounds)
			if userState.placeVisited(continent) {
				for country in continent.children {
					let countryBox = RegionBounds(regionHash: country.hashValue, bounds: country.geometry.aabb)
					worldTree.insert(value: countryBox, region: countryBox.bounds)
					if userState.placeVisited(country) {
						for region in country.children {
							if userState.placeVisited(continent) {
								let regionBox = RegionBounds(regionHash: region.hashValue, bounds: region.geometry.aabb)
								worldTree.insert(value: regionBox, region: regionBox.bounds)
							}
						}
					}
