Improved and simplified error handling.