Do a bit of refactoring and styling.

Okay, a lot of refactoring and styling.