Generify merge function