General style and compability improvements

I haven't changed the behaviour of the script in this PR. The commit messages probably describe my changes well enough. If you have any questions, I'll be glad to answer them.