Second try of my previous pull request, which was partially reverted due...

... to crashes and memory leaks