some refactoring in middlewares