Working version of db code