Unify MID generation and put it into mid_set.c