Improved error checking