Minor bug fix in merging function