Added some validation and better error reporting.