added some error handling and proper exit statuses