Fix global variable bug