added some error checking