KMeans out-of-bounds bug fix