Small middleware refactor