Fixed memory leaks by merging from kdtree