Refactor middlewares