Fix merge and add merge function