This is still making one pass over the data for each generated segment, there should be a one pass approach here too.