minor refactoring in middleware