moved global variables to function