Massive refactoring, compiles but will not pass tests