Fix termination bugs