Tidy up and rename some code