Pull in working code from Master

Hey @peschkaj please can you review and merge if you're cool with it? This works fine as part of my session state experiments.

OJ