Refactoring session handling

As per title.