     // MARK: Private functions to do all the work
     
     private func set(_ data: NSData?, key: String, accessibility: CFString = kSecAttrAccessibleWhenUnlocked) -> Bool {
+        
+         // The lock prevents the code to be run simultaneously
+         // from multiple threads which may result in crashing
+        lock.lock()
+        defer { lock.unlock() }
+        
         let hierKey = hierarchicalKey(key)
 
         let dict = service()

    }
    
    private func data(forKey key:String) -> Data? {
        let hierKey = hierarchicalKey(key)
        let query = self.query()
        query.setObject(hierKey, forKey: kSecAttrService as! NSCopying)

public class Strongbox {
    
    let keyPrefix: String?
    
    private let lock = NSLock()
    
    public var lastStatus = errSecSuccess
    
    public init() {
