modified calculateSegments