added code to gen_segs to deal with num = 0