There seems to be a lot of initialization going on in this *for* loop. It might make sense to split this out into its own function and handle the errors here, so there would be a single place in which to print errors.