Added better error checking