added code to compute centroid value and fixed some bugs in the code