Improve exception handling at the top level