Tidy up and remove unused code