added some return NULL statements on errors