Changed loop bounds for number of segments.