Updated the merge function