modified for-loop script