added error handling with error codes