Implemented the merge function