Tweaks to kmeans encoding function.