Separate most of middleware code