Rewrite error handling