             self.positions[wordIndex].append(positions[wordIndex])
         }
     }
+    
+    mutating func addIfNewReference(positions: [UInt8]) {
+        for wordIndex in 0..<positions.count {
+            let lastValue = self.positions[wordIndex].last
+            if lastValue == nil || lastValue != positions[wordIndex] {
+                self.positions[wordIndex].append(positions[wordIndex])
+            }
+        }
+    }
 }
 
 
 struct ConcordanceModel {
-    
-    struct TempWordRef : Equatable, Hashable {
-        let nodeId: String
-        var position: Int  // Should be unsigned byte, or unsigned short
-        
-        init(reference: String) {
-            let parts = reference.components(separatedBy: ";")
-            self.nodeId = parts[0]
-            self.position = Int(parts[1])!
-        }
-        
-        //var hashValue: Int {
-        //    return self.nodeId.hashValue
-        //}
-        
-        func hash(into hasher: inout Hasher) {
-            hasher.combine(self.nodeId)
-        }
-        
-        var reference: String {
-            get {
-                return self.nodeId + ";" + String(self.position)
-            }
-        }
-        
-        mutating func next() {
-            self.position += 1
-        }
-        
-        static func == (lhs: TempWordRef, rhs: TempWordRef) -> Bool {
-            //return lhs.nodeId == rhs.nodeId && lhs.position == rhs.position
-            return lhs.nodeId == rhs.nodeId
-        }
-    }
-    
-    //let wordsLookAhead = 0
     
     /**
     * This method searches for all verses that contain all of the words entered.

        return updatedPositions
    }
    
    private func matchToEachWord(wordPositions: WordPositions, index: Int) -> [UInt8]? {
        var updatedPositions: [UInt8] = Array(repeating: 0, count: wordPositions.numWords)
        var nextPosition: UInt8 = wordPositions.positions[0][index]
        updatedPositions[0] = nextPosition
        for wordIndex in 1..<wordPositions.numWords {
            let oneWordPositions: [UInt8] = wordPositions.positions[wordIndex]
            nextPosition += 1
            if !oneWordPositions.contains(nextPosition) {
                return nil
            }
            updatedPositions[wordIndex] = nextPosition
        }
        return updatedPositions
    }

     * This method searches for all verses that contain all of the words entered.
     * It also ensures that the words are in the sequence entered in the search.
     */
    func search3(bible: Bible, words: [String]) -> [WordRef] {
        var finalResult = [WordRef]()
        let results1: [WordRef] = self.search1(bible: bible, words: words)
        if results1.count == 0 {
            return finalResult
        }
        for index in 0..<results1.count {
            var wordRef = results1[index]
            let updatedPostions = self.matchToEachReference3(wordPositions: wordRef.wordPositions!)
            if updatedPostions.positions[0].count > 0 {
                wordRef.wordPositions = updatedPostions
                finalResult.append(wordRef)
            }
        }
        return finalResult
    }
    
    // This is identical to matchToEachReference2, except by calling matchToEachWord3
    private func matchToEachReference3(wordPositions: WordPositions) -> WordPositions {
        var updatedPositions = WordPositions(numWords: wordPositions.numWords)
        let firstWordPositions: [UInt8] = wordPositions.positions[0]
        for index in 0..<firstWordPositions.count {
            let matches: [UInt8]? = self.matchToEachWord3(wordPositions: wordPositions, index: index)
            if matches != nil {
                updatedPositions.addIfNewReference(positions: matches!)
            }
        }
        return updatedPositions
    }
    
    private func matchToEachWord3(wordPositions: WordPositions, index: Int) -> [UInt8]? {
        var updatedPositions: [UInt8] = Array(repeating: 0, count: wordPositions.numWords)
        var nextPosition: UInt8 = wordPositions.positions[0][index]
        updatedPositions[0] = nextPosition
        for wordIndex in 1..<wordPositions.numWords {
            let oneWordPositions: [UInt8] = wordPositions.positions[wordIndex]
            let greater: UInt8? = self.firstGreater3(array: oneWordPositions, num: nextPosition)
            if greater != nil {
                updatedPositions[wordIndex] = greater!
                nextPosition = greater!
            } else {
                return nil
            }
        }
        return updatedPositions
    }
        
    private func firstGreater3(array: [UInt8], num: UInt8) -> UInt8? {
        for val in array {
            if val > num {
                return val
            }
        }
        return nil
    }
}

