Fix memleak and simplify sdb_set