There seems to be a bug which has been introduced by this optimized version. Try Kmeans(X, 7) to reproduce.
Kmeans(X, 7)