Rewritten DatabaseHelper for efficiency.