Middleware: improve logic.