Added some error checks