Improve exception handling