Reduce variables scope, move declaration to first use point
Merge pull request #3492 from Dmitry-Me:reduceVariablesScope1