logic fixes with loop and bounds