replaced global variables to local