Simplified middleware, and added test for middleware