Merge algorithm