Updated script to execute writing the file only when called directly