use ref instead of pointer in inner loop