Added extra error checking