fixed variable scope bug