Document merge function