Code refactor: reduce the number of global variables.