Improved error checking and messages.