     /// - Throws: File related errors
     /// - Returns: An array of UploadMetadata types
     func loadAllMetadata() throws -> [UploadMetadata] {
-        let directoryContents = try FileManager.default.contentsOfDirectory(at: storageDirectory, includingPropertiesForKeys: nil)
-        
-        // if you want to filter the directory contents you can do like this:
-        let files = directoryContents.filter{ $0.pathExtension == "plist" }
-        let decoder = PropertyListDecoder()
-        
-        return files.compactMap { url in
-            if let data = try? Data(contentsOf: url) {
-                let metaData = try? decoder.decode(UploadMetadata.self, from: data)
-                        
-                // The documentsDirectory can change between restarts (at least during testing). So we update the filePath to match the existing plist again. To avoid getting an out of sync situation where the filePath still points to a dir in a different directory than the plist.
-                // (The plist and file to upload should always be in the same dir together).
-                metaData?.filePath = url.deletingPathExtension()
+        try queue.sync {
+
+            let directoryContents = try FileManager.default.contentsOfDirectory(at: storageDirectory, includingPropertiesForKeys: nil)
+            
+            // if you want to filter the directory contents you can do like this:
+            let files = directoryContents.filter{ $0.pathExtension == "plist" }
+            let decoder = PropertyListDecoder()
+            
+            let metaData: [UploadMetadata] = files.compactMap { url in
+                if let data = try? Data(contentsOf: url) {
+                    let metaData = try? decoder.decode(UploadMetadata.self, from: data)
+                    
+                    // The documentsDirectory can change between restarts (at least during testing). So we update the filePath to match the existing plist again. To avoid getting an out of sync situation where the filePath still points to a dir in a different directory than the plist.
+                    // (The plist and file to upload should always be in the same dir together).
+                    metaData?.filePath = url.deletingPathExtension()
+                    
+                    return metaData
+                }
                 
-                return metaData
-            }
-            
-            // Improvement: Handle error when it can't be decoded?
-            return nil
+                // Improvement: Handle error when it can't be decoded?
+                return nil
+            }
+            
+            return metaData
         }
     }
     

final class Files {
    
    let storageDirectory: URL
    
    /// Pass a directory to store the local cache in.
    /// - Parameter storageDirectory: Leave nil for the documents dir. Pass a relative path for a dir inside the documents dir. Pass an absolute path for storing files there.

    /// - Returns: The URL of the stored file
    @discardableResult
    func store(data: Data, id: UUID) throws -> URL {
        try queue.sync {
            guard !data.isEmpty else { throw FilesError.dataIsEmpty }
            try makeDirectoryIfNeeded()
            let fileName = id.uuidString
            
            let targetLocation = storageDirectory.appendingPathComponent(fileName)
            try data.write(to: targetLocation, options: .atomic)
            return targetLocation
        }
    }
    
    /// Removes metadata and its related file from disk
