Refactored middleware.