Fixed bugs with variables referenced from the wrong scrope.