fixed bugs related to global variable mistakes