Smarter error handler