corrected centroid calculation