lots of refactoring etc