Fix a couple loops in db.py