Much refactoring

Still lots more to be done