 				fs.writeBytes(_publicKey);
 				fs.close();
 				
-				st.sql = "INSERT INTO users (`name`, `nearid`, `key`, `publickey`)"
-					+ " VALUES ('"+File.userDirectory.name+"','"+_netConnection.nearID+"',?,?);";
-				st.setBinary(1, _privateKey);
-				st.setBinary(2, _publicKey);
+				st.sql = "INSERT INTO users (name, nearid, publickey)"
+					+ " VALUES ('"+File.userDirectory.name+"','"+_netConnection.nearID+"',@publicKey);";
+				st.parameters["@publicKey"] = _publicKey;
 			}
 			
 			trace("COMMUNICATIONS: Sending query to server..");
-			var t:MySqlToken = st.executeQuery();
-			t.addResponder(new AsyncResponder(mysqlNearIDUpdateSuccess, mysqlNearIDUpdateError, t));
-			
-			timerRefresh = new Timer(40000);
+			
+			var result = _databaseService.writeToDB(PUBLIC_KEY_DATABASE, st);
+			if (result is SQLResult)
+			{
+				dispatchEvent(new NetworkEvent(NetworkEvent.CONNECTED, _netConnection.nearID));
+			}
+			else if (result is SQLError)
+			{
+				dispatchEvent(new NetworkEvent(NetworkEvent.ERROR, null));
+				trace("COMMUNICATIONS: Update error." + result);
+			}
+			
+			timerRefresh = new Timer(20000);
 			timerRefresh.addEventListener(TimerEvent.TIMER, networkTimerRefresh);
 			timerRefresh.start();
 		}

			}
		}
		
		private function mysqlNearIDUpdateError(info:Object, token:MySqlToken):void 
		{
			dispatchEvent(new NetworkEvent(NetworkEvent.ERROR, null));
			trace("COMMUNICATIONS: Update error.");
		}
		
		private function mysqlNearIDUpdateSuccess(data:Object, token:MySqlToken):void 
		{
			trace("COMMUNICATIONS: Update success.");
			dispatchEvent(new NetworkEvent(NetworkEvent.CONNECTED, _netConnection.nearID));
		}
		
		public function get privateKey():ByteArray 
		{

				fs.readBytes(_publicKey, 0, 24); //Read public key into identity
				fs.close();
				
				st.text = "UPDATE users "
				+ "SET nearid="+_netConnection.nearID+" "
				+ "WHERE key=@publicKey;";
				st.parameters["@publicKey"] = _publicKey;
			}
			else //Register
			{
