Refactoring and code tidying

Various refactoring and code cleaning from recent travel with limited internet