trying to fix output but having trouble with local vs. global variables