Several error handling improvements