major refactor of db interface