fixed write-script() function