cleanup code a bit

Some formatting, some logic
I call it Pascal style ;-) and I prefer to define variables closer to its usage.