Improve the loader a bit

Clean up the loader to open the file only once and pass the IOFile around to functions requiring it, and add a real file detection function based on its magic.
I don’t have enough data to find out if it’s needed or not, but it does look like a debug test left here, I can remove it if someone can test that.