Fixed some errors in the session handling logic