Code clean up

This mostly fixes missing or inappropriate scopes of loop variables in
for-loops. This means all loop variables, unless still needed AFTER a
loop's body, should now be declared using the `let' keyword.

Signed-off-by: Niklas Koep <niklas.koep@gmail.com>