factoring out inner loop ops