Refactor merge function for mao