Remove global variables and clean up code.