Fixing code to generate spreadsheets