Fix session handling, #1163 and #1164.