This version seems to work but the allocation of segments has low utilization. Probably best to allocate in chunks rather than try to make them contiguous.