refactor session logic