Refactor architecture to use middlewares