Get rid of one inner loop SQL statement, speeding things up a bit.