Moved some common session related functions in here.