Code tidy-up and tweaks