refactored middleware setting