Make this less generic and just do what you need to do on the specific files with problems. It's quite hard to follow as-is. Also: needs a comment.