Added extra error checks