kajor rewrite of db code
Merge pull request #1 from araroot/master

changed indexing logic by using primary keys instead