Implemented most of session management code.