Some tidy-ups, refactorings and missing tests