Some tidy up and optimisation