Accomodating multiple sessions and a bit of refactoring