Total rewrite, db works now