Fix double execution of the terminate function