implemented merge function