db: clean up the code and export some more functions