Adding null checks to avoid NPE in merging