Only open database just in time instead of holding an instance

Fixes #1