Massive refactoring; my old code is truly hideous