refactored kmeans