added error checking after session_loop