Bugfix: memory and thread-deleting problems