implement working kd tree builder