replaced second forloop