Fixing deadlock caused by terminate