update cleanup script

update cleanup script, change position of some code.