lots of bloddy refactoring