moved session finding logic to function.