Refactored the script, now it's possible to call the code multiple times on multiple files. Still needs modifications to make this usable for the user.