                 .filter { $0.0 == peripheral.peripheral }
                 .map { (_, error) -> T in
                     throw BluetoothError.peripheralDisconnected(peripheral, error)
-            }
+                }
         }
     }
 

                    return peripheralTable.map {
                        Peripheral(manager: strongSelf, peripheral: $0)
                    }
            }
        }
        return ensure(.poweredOn, observable: observable)
    }

    /// - returns: Infinite stream of scanned peripherals.
    public func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, options: [String: Any]? = nil)
        -> Observable<ScannedPeripheral> {
        return .deferred { [weak self] in
            guard let strongSelf = self else { throw BluetoothError.destroyed }
            let observable: Observable<ScannedPeripheral> = { [weak self] () -> Observable<ScannedPeripheral> in
                guard let strongSelf = self else { return .error(BluetoothError.destroyed) }
                // If it's possible use existing scan - take if from the queue
                strongSelf.lock.lock(); defer { strongSelf.lock.unlock() }
                if let elem = strongSelf.scanQueue.first(where: { $0.shouldAccept(serviceUUIDs) }) {
                    guard let serviceUUIDs = serviceUUIDs else {
                        return elem.observable
                    }

                    // When binding to existing scan we need to make sure that services are
                    // filtered properly
                    return elem.observable.filter { scannedPeripheral in
                        if let services = scannedPeripheral.advertisementData.serviceUUIDs {
                            return Set(services).isSuperset(of: Set(serviceUUIDs))
                        }
                        return false
                    }
                }

                let scanOperationBox = WeakBox<ScanOperation>()

                // Create new scan which will be processed in a queue
                let operation = Observable.create { [weak self] (element: AnyObserver<ScannedPeripheral>) -> Disposable in
                    guard let strongSelf = self else { return Disposables.create() }
                    // Observable which will emit next element, when peripheral is discovered.
                    let disposable = strongSelf.centralManager.rx_didDiscoverPeripheral
                        .flatMap { [weak self] (peripheral, advertisment, rssi) -> Observable<ScannedPeripheral> in
                            guard let strongSelf = self else { throw BluetoothError.destroyed }
                            let peripheral = Peripheral(manager: strongSelf, peripheral: peripheral)
                            let advertismentData = AdvertisementData(advertisementData: advertisment)
                            return .just(ScannedPeripheral(peripheral: peripheral,
                                                           advertisementData: advertismentData, rssi: rssi))
                        }
                        .subscribe(element)

                    // Start scanning for devices
                    strongSelf.centralManager.scanForPeripherals(withServices: serviceUUIDs, options: options)

                    return Disposables.create { [weak self] in
                        guard let strongSelf = self else { return }
                        // When disposed, stop all scans, and remove scanning operation from queue
                        strongSelf.centralManager.stopScan()
                        disposable.dispose()
                        do { strongSelf.lock.lock(); defer { strongSelf.lock.unlock() }
                            if let index = strongSelf.scanQueue.index(where: { $0 == scanOperationBox.value! }) {
                                strongSelf.scanQueue.remove(at: index)
                            }
                        }
                    }
                }
                .queueSubscribe(on: strongSelf.subscriptionQueue)
                .publish()
                .refCount()

                let scanOperation = ScanOperation(uuids: serviceUUIDs, observable: operation)
                strongSelf.scanQueue.append(scanOperation)

                scanOperationBox.value = scanOperation
                return operation
            }()
            // Allow scanning as long as bluetooth is powered on
            return strongSelf.ensure(.poweredOn, observable: observable)
        }
    }

    // MARK: State
