Merge pull request #1587 from kumpera/cleanup_threading