cleaning and adding some defunctionalisation