Another major refactoring

Another major refactoring in the structure of the code.