Massive refactoring, almost done