Corrected some global variables change to local variables