structured error handler + little cleanup of code in server