 
 // MARK: Private Internal methods
 private extension FayeClient {
-  func subscribeQueuedSubscriptions(){
+  func subscribeQueuedSubscriptions() {
     // if there are any outstanding open subscriptions resubscribe
-    if(self.queuedSubscriptions.count > 0){
-      let queue:NSSet = self.queuedSubscriptions.copy() as! NSSet
-
-      for channel in queue{
+    if let queue:NSSet = self.queuedSubscriptions.copy() as? NSSet where self.queuedSubscriptions.count > 0 {
+      for channel in queue {
         self.queuedSubscriptions.removeObject(channel)
         self.subscribe(channel as! String)
       }
     }
   }
 
-  func send(message: NSDictionary){
+  func send(message: NSDictionary) {
     // Parse JSON
     if let string = JSON(message).rawString() {
       self.transport?.writeString(string)
     }
   }
 
-  func receive(message: String){
+  func receive(message: String) {
     // Parse JSON
-    let jsonData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
-    let json = JSON(data: jsonData!)
-    self.parseFayeMessage(json)
+    if let jsonData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
+      let json = JSON(data: jsonData)
+      self.parseFayeMessage(json)
+    }
   }
 
   // http://iosdevelopertips.com/swift-code/base64-encode-decode-swift.html
   func nextMessageId() -> String{
     self.messageNumber++
-    if(self.messageNumber >= UINT32_MAX){
+    if self.messageNumber >= UINT32_MAX {
       messageNumber = 0
     }
     let str = "\(self.messageNumber)"

  // "clientId": "Un1q31d3nt1f13r",
  // "subscription": "/foo/**"
  // }
  func unsubscribe(channel:String){
    if let clientId = self.fayeClientId {
      let dict:[String:AnyObject] = ["channel": BayeuxChannel.UNSUBSCRIBE_CHANNEL.description, "clientId": clientId, "subscription": channel]
      if let string = JSON(dict).rawString() {
        self.transport?.writeString(string)
      }

    self.transport = WebsocketTransport(url: aFayeURLString)
    self.transport!.delegate = self;

    if let chan = channel {
      self.queuedSubscriptions.addObject(chan)
    }
    self.connectionInitiated = false
  }

  public convenience init(aFayeURLString:String, channel:String, channelBlock:ChannelSubscriptionBlock) {
    self.init(aFayeURLString: aFayeURLString, channel: channel)
    self.channelSubscriptionBlocks[channel] = channelBlock;
  }

  public func connectToServer() {
    if self.connectionInitiated != true {
      self.transport?.openConnection()
      self.connectionInitiated = true;
    }
  }

  public func disconnectFromServer() {
    self.disconnect()
  }

  public func sendMessage(messageDict: NSDictionary, channel:String) {
    self.publish(messageDict as! Dictionary, channel: channel)
  }

  public func sendMessage(messageDict:[String:AnyObject], channel:String) {
    self.publish(messageDict, channel: channel)
  }

  public func subscribeToChannel(channel:String) {
    if self.isSubscribedToChannel(channel) || self.pendingSubscriptions.containsObject(channel) {
      return
    }

    if self.fayeConnected == true {
      self.subscribe(channel)
    } else {
      self.queuedSubscriptions.addObject(channel)
    }
  }

  public func subscribeToChannel(channel:String, block:ChannelSubscriptionBlock) {
    self.subscribeToChannel(channel)
    self.channelSubscriptionBlocks[channel] = block;
  }

  public func unsubscribeFromChannel(channel:String) {
    self.queuedSubscriptions.removeObject(channel)
    self.unsubscribe(channel)
    self.channelSubscriptionBlocks[channel] = nil;
