More changes to main, mostly refactoring. Henrik karl