Refactored db opening, removed some old cruft