major cleanup and optimisation pass