Sessions refactoring
Fixes #1527