Fixed wrong iteration logic