added kd trees and made patches