using flag to break multiple nest