Improved error checking.