Make the logic a bit more robust and readble
Put protection for session functions even here