Reduce every declaration scope to minimum

I used cppCheck to detect when the scope of a variable can be reduced,
and reduced all of them.

No functional change.