refactoring to handle multiple middlewares