Merge "Fix various thread and memory leaks"