Don't do DB calls in the middle of a loop!!!

Signed-off-by: Suki <suki@missallsunday.com>