Updated to include better error handling and to use the new error class.