added some nil checks