refactor database and add tests