 
 
 importFromV1 = !->
-	Db.shared.set 'x_v1backup', Db.shared.peek()
+	log "Converting database of old version to new"
+	Db.backend.set 'v1backup', Db.shared.peek()
 	Db.shared.iterate (key) !->
-		if key.key() isnt 'x_v1backup'
-			Db.shared.remove key.key()
-	log "Converting database of old version to new"
-	Db.shared.iterate "x_v1backup", "transactions", (transaction) !->
+		key.remove()
+	Db.shared.set "balances", "V2"
+	Db.backend.iterate "v1backup", "transactions", (transaction) !->
+		old = Db.shared.peek('transactionId')
 		id = Db.shared.modify 'transactionId', (v) -> (v||0)+1
-		log "old transaction: "+transaction.key()
+		log "old transaction: "+transaction.key()+", key from="+old+", to="+id
+		#Db.shared.set "transactions", id, id+"-hi"
 		Db.shared.set "transactions", id, "creatorId", transaction.peek("creatorId")
 		Db.shared.set "transactions", id, "text", transaction.peek("description")
 		Db.shared.set "transactions", id, "created", transaction.peek("time")
-		total = (transaction.peek("cents")/100)
+		total = (transaction.peek("cents")/100.0)
 		Db.shared.set "transactions", id, "total", total
 		forData = {}
 		transaction.iterate "borrowers", (user) !->
 			forData[user.key()] = true
 		Db.shared.set "transactions", id, "for", forData
 		byData = {}
-		byData[transaction.peek("lenderId")] = true
+		byData[transaction.peek("lenderId")] = total
 		Db.shared.set "transactions", id, "by", byData
-		balanceAmong total, byData, id
-		balanceAmong -total, forData, id
+		log "byData balanceAmong:"
+		balanceAmong total, byData
+		log "forData balanceAmong:"
+		balanceAmong -total, forData
 
 randomFromSeed = (seed) ->
 	x = Math.sin(seed) * 10000

			id = Db.shared.modify 'transactionId', (v) -> (v||0)+1
			transaction = {}
			forData = {}
			forData[to] = true
			byData = {}
			byData[from] = true
			transaction["creatorId"] = -1
			transaction["for"] = forData
			transaction["by"] = byData


# Process a transaction and update balances
balanceAmong = (total, users, txId = 99) !->
	log "balanceAmong: total="+total+", users="+JSON.stringify(users)+", txId="+txId
	divide = []
	remainder = total
	totalShare = 0
