Refactoring session handling + minor bugfixes