Refactored centroid and boundary code