updated endSession logic