cleaning and optimizing algorithms