added extra error checking