Some bug fixes for middleware
Adding middleware