fixed some issues with outsourced loop