general refactor, added consistency checks and some methods to manage sessions