Made function `each_segment` to split up the code into more bit-sized chunks, moved loop through the data over each segment into that function.