added more error checks