updated the merge function