Fix middleware code to allow using it without going through Router class / HTTP