added some nil checks to avoid crashing on bad input