Improved error catching.