Add a null check to fix a minor race condition