Some refactoring of the Master

I did some refactoring of the master, to make the code somewhat cleaner.