Refactored session handling.