fix middleware ordering issue