modified the code for centroid