Stabilizing kmeans code