Fix bug in terminate