Added some more detailed error checking