refactor find-closet-centroid function