refactor subdb code