Maybe it's better to remove this `ref` and add it as an argument to `loop`?