fixed exception, fixed variable scope
merge and fix indent