Tons of variable scopes eliminated and some other stuff

Signed-off-by: Harry Young <hendrik.gerritzen@googlemail.com>