added proper global variable scoping