Major refactoring.
Still some large improvement to the code base.