fixed variable scope
code formatting