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

... to caused crashes and memory leaks