Handle problems with mixed line endings

This commit is based on fulldecent's pull request, and adds comments to
make the script a little bit more legible.