Code tidy ups

Minor tidy-ups and refactorings.