Fix crashes relating to nil objects