Fix session middleware bugs.