Fix bug in termination condition