Major refactoring/tidy up