 		let rightResult: Result
 		if kdCompare(u: q, v: v, d: d) {
 			leftResult = kdFindNearest(query: q, node: l, d: d.flip(),
-																 aabb: bbTrimLow(d: d, value: v.x, bb: aabb),
+																 aabb: bbTrimLow(d: d, value: v.p.x, bb: aabb),
 																 result: newResult)
 			rightResult = kdFindNearest(query: q, node: r, d: d.flip(),
-																	aabb: bbTrimHigh(d: d, value: v.x, bb: aabb),
+																	aabb: bbTrimHigh(d: d, value: v.p.x, bb: aabb),
 																	result: newResult)
 		} else {
 			rightResult = kdFindNearest(query: q, node: r, d: d.flip(),
-																	aabb: bbTrimHigh(d: d, value: v.x, bb: aabb),
+																	aabb: bbTrimHigh(d: d, value: v.p.x, bb: aabb),
 																	result: newResult)
 			leftResult = kdFindNearest(query: q, node: l, d: d.flip(),
-																 aabb: bbTrimLow(d: d, value: v.x, bb: aabb),
+																 aabb: bbTrimLow(d: d, value: v.p.x, bb: aabb),
 																 result: newResult)
 		}
 		

}

// MARK: Algoritm
func kdInsert(v: Vertex, n: KDNode, d: SplitDirection = .x) -> KDNode {
	switch n {
	case .Empty: return KDNode(left: .Empty, v, right: .Empty, d)
	case .Split(let left, let a, let right, let d):
		guard a != v else { return n }
		if kdCompare(u: v, v: a, d: d) {
			return n.replaceLeft(newLeft: kdInsert(v: v, n: left, d: d.flip()))
		} else {

}

// MARK: Helpers
fileprivate func kdCompare(u: PointForm, v: PointForm, d: SplitDirection) -> Bool {
	switch d {
	case .x: return u.p.x < v.p.x
	case .y: return u.p.y < v.p.y
	}
}

fileprivate func sqDist(v: PointForm, q: PointForm) -> Double {
	return Double(pow(v.p.x - q.p.x, 2.0) + pow(v.p.y - q.p.y, 2.0))
}

fileprivate func bbDist(v: PointForm, bb: CGRect) -> Double {
	let dx = max(abs(CGFloat(v.p.x) - bb.midX) - bb.width / 2.0, 0.0)
	let dy = max(abs(CGFloat(v.p.y) - bb.midY) - bb.height / 2.0, 0.0)
	return Double(dx * dx + dy * dy)
}

