Changed working with sessions. Do not create a session until necessary