Turned extracting and cleaning algorithms into functions