refactored all the global variables to more clearly reflect their usage.