Reverted back to old way of working with custom middleware, for (easier) backwards compatibility