rewrite code to not have warnings about not defined indexes, variables,
or not being able to write files