Move some global variables to local.
code clean up