Major refactorings for the sake of cleaning up.