Handle ctrl-c properly with threads