refactored db logic into separate module, implemented some tests