Improve handling of exceptions