Fix issues with loop