restructuring of merge function