     
     private var peripheralManager: CBPeripheralManager!
     
-    private var region: CLBeaconRegion!
+    private var region: BeaconRegion!
     
-    private static let regionIdentifier = NSUUID().UUIDString
+    private static let regionIdentifier = UUID().uuidString
     
     public override init() {
         super.init()
-        peripheralManager = CBPeripheralManager(delegate: self, queue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))
+        peripheralManager = CBPeripheralManager()
+        peripheralManager.delegate = self
+    }
+    
+    deinit {
+        stopAdvertising()
     }
     
     /// Start advertising the beacon.
     ///
     /// NOTE: CoreBluetooth only allows virtual beacons while the app is in
-    ///       the foreground, so VirtualBeacon set idleTimerDisabled to true
+    ///       the foreground, so VirtualBeacon sets idleTimerDisabled to true
     ///       while it is advertising.
     ///
     /// - Parameters:

//
// Copyright © 2015 Apparata AB. All rights reserved.
//
// MIT License
//
// Copyright (C) 2015 by Apparata AB
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal


/// Delegate of the VirtualBeacon class.
public protocol VirtualBeaconDelegate : class {
    func virtualBeaconBluetoothPoweredOn(_ virtualVeacon: VirtualBeacon)
    func virtualBeaconDidStartAdvertising(_ virtualBeacon: VirtualBeacon)
    func virtualBeaconDidFailToStartAdvertising(_ virtualBeacon: VirtualBeacon, error: NSError)
    func virtualBeaconDidFailToStartAdvertisingDueToBluetoothNotEnabled(_ virtualBeacon: VirtualBeacon)
    func virtualBeaconDidStopAdvertising(_ virtualBeacon: VirtualBeacon)
}

#if os(tvOS)
    private struct BeaconRegion {
        let proximityUUID: NSUUID
        let major: UInt16
        let minor: UInt16
        let identifier: String
        
        init(proximityUUID: NSUUID, major: UInt16, minor: UInt16, identifier: String) {
            self.proximityUUID = proximityUUID
            self.major = major
            self.minor = minor
            self.identifier = identifier
        }
        
        func peripheralData(withMeasuredPower power: Int8?) -> [String: AnyObject] {
            let iBeaconKey = "kCBAdvDataAppleBeaconKey"
            
            let data = NSMutableData(capacity: 21)!
            var uuidBytes = [UInt8](repeating: 0, count: 16)
            proximityUUID.getBytes(&uuidBytes)
            data.append(uuidBytes, length: 16)
            var majorValue = CFSwapInt16(major)
            data.append(&majorValue, length: 2)
            var minorValue = CFSwapInt16(minor)
            data.append(&minorValue, length: 2)
            var powerValue: Int8 = power ?? -59
            data.append(&powerValue, length: 1)
            
            return [iBeaconKey: data]
        }
    }
#else
    typealias BeaconRegion = CLBeaconRegion
#endif

/// VirtualBeacon allows a iOS device to act as an iBeacon.
public class VirtualBeacon: NSObject, CBPeripheralManagerDelegate {
