Refactored the session handling.