Improved the error checking.