Tested and improved new db code