Improved kd-tree building function (Fixed usage of Surface Area Heuristic)