Fixed issues in middleware to handle request correctly