General tidy-up of untidy code