added some more error checks