Major refactor to improve token and session management