refactor dispatcher middleware