made KD tree construction smarter