Major refactoring

We've gone a long way in the refactor branch: let's continue with the development in the master