updated version with session management