ported some merge code