Split sdb.c into query.c, minor cleanups