Rewrite with new database API