Fixed merge function