More middleware refactoring.