Added some more error checks.