optimize imgs and set anonymous function to script to make sure no gloabal vars exist