revised with loop