Adding session management and cleaning up database code