 
     @discardableResult
     func charm(with context: GraphicsContext, toFrame index: Int?) -> Int {
-        // ⭐️ Lock while reading commandHistories.
-        defer { self.lock.unlock() }
-        self.lock.lock()
-
         let endIndex = commandHistories.count - 1
         let toIndex = min(max((index ?? endIndex), 0), endIndex)
 


    private(set) var tortoises: [Tortoise] = []
    private(set) var commandHistories: [(tortoiseTag: Int, commandIndex: Int)] = []

    // ⭐️ Add lock object.
    private let lock = NSLock()

    var commandedHandler: ((TortoiseCharmer) -> Void)?


            states[index].canvasSize = context.size
        }

        // Make a copy of the array of commands for each Tortoise
        var commandsToRun = [ Int: [Command] ]()
        for (index, tortoise) in tortoises.enumerated() {
            commandsToRun[index] = tortoise.commands
        }

        // Now iterate over all the commands in the command history and run
        // using the copy of commands for each tortoise (saved a moment ago)
        for (index, history) in commandHistories.enumerated() where index <= toIndex {

            // Get commands out of the dictionary for the current tortoise
            if let commands = commandsToRun[history.tortoiseTag] {

                // Actually run the command at the given index
                states[history.tortoiseTag] = commands[history.commandIndex]
                    .exexute(in: states[history.tortoiseTag], with: context.cgContext)

            }
        }

        for index in 0..<tortoises.count {
