Quick fix to only open the database a few times

Opening the database was the biggest bottleneck