reworked merge code