improved middleware