fixed merging algorithm