try to fix inner loop logic some more