Fix variable hoisting bug