updated merging function (Michele)