fixed a bug and optimized kmeans clustering