Refactor global variables into class members and other minor tewaks