Update for cleaner code. Add more session control. Fix minor bugs.