Refactor and customize session

Separate some concerns and prepare to make the session attribute an official part of the API.