fixed merge function