added some additional error checking.