Add a few error checks to avoid crashing too soon