refactored session code to make it more reusable