Refactoring of `middleware`