added merging algorithm