Refactoring session handling

* Refactoring to inject dependencies
* Cleanup code