Added generic error handler function