Modified algorithm for environment as well as termination case