Fix missed code from last commit
added additional error checking