Extract out merge implementation