Implement merge function