Better error and close handling.