Took some fixes for session handling from the current Bergmannian master.