moved database code to lib/db