             let data = try Data(contentsOf: location)
             if let image = UIImage(data: data), let url = downloadTask.currentRequest?.url {
                 cache.setObject(image, forKey: url.absoluteString as AnyObject, cost: data.count)
-                if let _ = downloadTaskByURLString[url.absoluteString] {
-                    downloadTaskByURLString.removeValue(forKey: url.absoluteString)
+                if let _ = getDownloadTask(for: url) {
+                    removeDownloadTask(for: url)
                 }
                 guard let completions: [ImageStoreCompletionHandler] = completionsByURLString[url.absoluteString] else {
                     return

        }

        let task: URLSessionDownloadTask
        if let _task = downloadTaskByURLString[url.absoluteString] {
            task = _task
            task.resume()
        } else {
            task = session.downloadTask(with: url)
            task.resume()
            downloadTaskByURLString[url.absoluteString] = task
        }

        return task
    }

    public func suspendIfResuming(url: URL) {
        if let task = downloadTaskByURLString[url.absoluteString] {
            task.suspend()
        }
    }

    public func cancel(url: URL) {
        if let task = downloadTaskByURLString[url.absoluteString] {
            task.cancel()
            downloadTaskByURLString.removeValue(forKey: url.absoluteString)
        }
    }

}

extension ImageStore: URLSessionDownloadDelegate {

        guard let completions: [ImageStoreCompletionHandler] = completionsByURLString[url.absoluteString] else {
            return
        }
        if let _ = getDownloadTask(for: url) {
            removeDownloadTask(for: url)
        }
        DispatchQueue.main.async { [weak self] in
            completions.forEach { $0(nil, .taskError(error)) }
