refactoring. fixed errors with running middlewares