fixed to work as middleware.