Implemented update_centroids and various helper functions.