Code cleanup and some session functionality