fixing logic

fixing logic with loop