Mostly finished the refactoring.