Major refactoring, almost done.