Refactor session logic into functions.

Locating all of the session state in the sessionWrapper allows
for much lower coupling between components in session logic.