Refactoring - Stage 1.

Cleanup and organise code before it gets messy.