fixed bug in merge function