middleware fixes