General tidy-up of code