Added termination condition checking