added some more error checking