add termination condition. fatal bug fixed.