Refactor / reformat code

Declare variables near use, declare variables in loops, less brevity, indenting