Massive refactoring for tidiness and general DRY