Remove some unused variables and reduce number of global vars