             "ZMISBEHAVIN integer," +
             "ZPORT integer," +
             "ZSERVICES integer," +
-            "ZTIMESTAMP timestamp)", nil, nil, nil) != SQLITE_OK ||
-            sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZADDRESS_INDEX on ZBRPEERENTITY (ZADDRESS)",
-                         nil, nil, nil) != SQLITE_OK ||
-            sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZMISBEHAVIN_INDEX " +
-                "on ZBRPEERENTITY (ZMISBEHAVIN)", nil, nil, nil) != SQLITE_OK ||
-            sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZPORT_INDEX on ZBRPEERENTITY (ZPORT)",
-                         nil, nil, nil) != SQLITE_OK ||
-            sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZTIMESTAMP_INDEX on ZBRPEERENTITY (ZTIMESTAMP)",
-                         nil, nil, nil) != SQLITE_OK {
-            print(String(cString: sqlite3_errmsg(db)))
-        }
-        
-        if sqlite3_exec(db, "create table if not exists Z_PRIMARYKEY (" +
+            "ZTIMESTAMP timestamp)", nil, nil, nil)
+        if r == SQLITE_OK {
+            r = sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZADDRESS_INDEX on ZBRPEERENTITY (ZADDRESS)",
+                             nil, nil, nil)
+        }
+        if r == SQLITE_OK {
+            r = sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZMISBEHAVIN_INDEX " +
+                "on ZBRPEERENTITY (ZMISBEHAVIN)", nil, nil, nil)
+        }
+        if r == SQLITE_OK {
+            r = sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZPORT_INDEX on ZBRPEERENTITY (ZPORT)",
+                             nil, nil, nil)
+        }
+        if r == SQLITE_OK {
+            r = sqlite3_exec(db, "create index if not exists ZBRPEERENTITY_ZTIMESTAMP_INDEX " +
+                "on ZBRPEERENTITY (ZTIMESTAMP)", nil, nil, nil)
+        }
+        if r != SQLITE_OK { print(String(cString: sqlite3_errmsg(db))) }
+
+        // primary keys
+        r = sqlite3_exec(db, "create table if not exists Z_PRIMARYKEY (" +
             "Z_ENT INTEGER PRIMARY KEY," +
             "Z_NAME VARCHAR," +
             "Z_SUPER INTEGER," +
-            "Z_MAX INTEGER)", nil, nil, nil) != SQLITE_OK {
-            print(String(cString: sqlite3_errmsg(db)))
-        }
-        
-        // TODO: XXX populate Z_PRIMARYKEY
+            "Z_MAX INTEGER)", nil, nil, nil)
+        if r == SQLITE_OK {
+            r = sqlite3_exec(db, "insert into Z_PRIMARYKEY (Z_ENT, Z_NAME, Z_SUPER, Z_MAX) " +
+                "select 6, 'BRTxMetadataEntity', 0, 0 except" +
+                "select 6, Z_NAME, 0, 0 from Z_PRIMARYKEY where Z_NAME = 'BRTxMetadataEntity'", nil, nil, nil)
+        }
+        if r == SQLITE_OK {
+            r = sqlite3_exec(db, "insert into Z_PRIMARYKEY (Z_ENT, Z_NAME, Z_SUPER, Z_MAX) " +
+                "select 2, 'BRMerkleBlockEntity', 0, 0 except" +
+                "select 2, Z_NAME, 0, 0 from Z_PRIMARYKEY where Z_NAME = 'BRMerkleBlockEntity'", nil, nil, nil)
+        }
+        if r == SQLITE_OK {
+            r = sqlite3_exec(db, "insert into Z_PRIMARYKEY (Z_ENT, Z_NAME, Z_SUPER, Z_MAX) " +
+                "select 3, 'BRPeerEntity', 0, 0 except" +
+                "select 3, Z_NAME, 0, 0 from Z_PRIMARYKEY where Z_NAME = 'BRPeerEntity'", nil, nil, nil)
+        }
+        if r == SQLITE_OK { r = sqlite3_prepare_v2(db, "select Z_ENT, Z_NAME from Z_PRIMARYKEY", -1, &sql, nil) }
+        if r == SQLITE_OK { r = sqlite3_step(sql) }
+        
+        while r == SQLITE_ROW {
+            let name = String(cString: sqlite3_column_text(sql, 1))
+            
+            if name == "BRTxMetadataEntity" { txEnt = sqlite3_column_int(sql, 0) }
+            else if name == "BRTxMerkleBlockEntity" { blockEnt = sqlite3_column_int(sql, 0) }
+            else if name == "BRPeerEntity" { peerEnt = sqlite3_column_int(sql, 0) }
+            r = sqlite3_step(sql)
+        }
+        
+        if r == SQLITE_DONE { r = sqlite3_finalize(sql) }
+        if r != SQLITE_OK { print(String(cString: sqlite3_errmsg(db))) }
     }
     
     private func loadTransactions() -> [UnsafeMutablePointer<BRTransaction>?] {
         var sql: OpaquePointer? = nil
         var transactions = [UnsafeMutablePointer<BRTransaction>?]()
-
-        if sqlite3_prepare_v2(db, "select ZBLOB from ZBRTXMETADATAENTITY where ZTYPE = 1", -1, &sql, nil) != SQLITE_OK {
-            print(String(cString: sqlite3_errmsg(db)))
-        }
-        
-        while sqlite3_step(sql) == SQLITE_ROW {
+        var r: Int32
+
+        r = sqlite3_prepare_v2(db, "select ZBLOB from ZBRTXMETADATAENTITY where ZTYPE = 1", -1, &sql, nil)
+        if r == SQLITE_OK { r = sqlite3_step(sql) }
+        
+        while r == SQLITE_ROW {
             let buf = sqlite3_column_blob(sql, 0)
             let len = Int(sqlite3_column_bytes(sql, 0))
             let tx = BRTransactionParse(buf?.assumingMemoryBound(to: UInt8.self), len - MemoryLayout<UInt32>.size*2)

        // setup tables and indexes
        setupDb()
        
        // instatiate wallet
        var transactions = loadTransactions()
        wallet = BRWalletNew(&transactions, transactions.count, masterPubKey)


    
    // create tables and indexes (these are inherited from CoreData)
    private func setupDb() {
        var sql: OpaquePointer? = nil
        var r: Int32
        
        // tx table
        r = sqlite3_exec(db, "create table if not exists ZBRTXMETADATAENTITY (" +
            "Z_PK integer primary key," +
            "Z_ENT integer," +
            "Z_OPT integer," +
            "ZTYPE integer," +
            "ZBLOB blob," +
            "ZTXHASH blob)", nil, nil, nil)
        if r == SQLITE_OK {
            r = sqlite3_exec(db, "create index if not exists ZBRTXMETADATAENTITY_ZTXHASH_INDEX " +
                "on ZBRTXMETADATAENTITY (ZTXHASH)", nil, nil, nil)
        }
        if r == SQLITE_OK {
            r = sqlite3_exec(db, "create index if not exists ZBRTXMETADATAENTITY_ZTYPE_INDEX " +
                "on ZBRTXMETADATAENTITY (ZTYPE)", nil, nil, nil)
        }
        if r != SQLITE_OK { print(String(cString: sqlite3_errmsg(db))) }
        
        // blocks table
        r = sqlite3_exec(db, "create table if not exists ZBRMERKLEBLOCKENTITY (" +
            "Z_PK integer primary key," +
            "Z_ENT integer," +
            "Z_OPT integer," +
