Clean up looping, using new db functions