iteration logic improved