changed some global variables to local