improved readSegment