refactor scrape vars, and fs write outside loop