Remove globals, code cleanup