removed global variable and simplified