Added conditions in duplicate merging to avoid calling methods on nil.