Function to compute new centroids