rewrite db handling