Code style fixes.

* Reduce variable scoping - especially in loop indexes.
* Remove unnecessary semicolon from the end of line
* Reduce variable scope to improve readability and maintainability