Patch by Michael McFadden, with a few small style tweaks.
Attach a file by drag & drop or click to upload