Refactors trimBoundary, expandBoundary