Optimized middleware code