Refactored the code to make it easier to follow

Hey Jeff

I was bored so had a stab at further refactoring the code.

Not that your way was *wrong*, but this is a lot closer to how I would write it.

Please feel free to ask if any of my changes are unclear.