Refactoring: Middleware boilerplate