improved open/close database logic.