improved the error handling