Start code to calculate segments.