Fix wrong merging algorithm