kajor rewrite of db code