all sorts of refactoring and fixes