clean up and optimize script