There's a better loop implementation available:
https://docs.arcavias.com/index.php/Developers/Fetch_records_in_bunches