better loop/divide handling