Pull request to master

Done (and checked for potential crashes/bugs). Fortunately, I  didn't find any.

Thanks for this. Clean and very simple compared to the one I was using.