         }
         return result.sorted()
     }
-    
-    func tableView(_ tableView: UITableView,
-                   titleForHeaderInSection section: Int) -> String? {
-        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
-            if self.section == nil {
-                //let bookId = ConcordanceModel.shared.resultsByBook[section][0].bookId
-                //let bible = HistoryModel.shared.currBible
-                //if let name = bible.tableContents?.getBook(bookId: bookId)?.name {
-                //    return name
-                //} else {
-                return "  "
-                //}
-            }
-        }
-        return nil
-    }
-    
-    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
-        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
-            return false
-        } else {
-            return true
-        }
-    }
-    
-    func tableView(_ tableView: UITableView,
-                   commit editingStyle: UITableViewCell.EditingStyle,
-                   forRowAt indexPath: IndexPath) {
-        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_SEARCHES {
-            ConcordanceModel.shared.removeHistory(row: indexPath.row)
-            tableView.deleteRows(at: [indexPath], with: .automatic)
-        }
-    }
-    
-    //
-    // Delegate
-    //
-    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
-            let cell = tableView.cellForRow(at: indexPath) as! ConcordanceResultCell
-            if cell.verse.text != nil {
-                let wordRef = ConcordanceModel.shared.resultsByBook[indexPath.section][indexPath.row]
-                HistoryModel.shared.changeReference(bookId: wordRef.bookId, chapter: Int(wordRef.chapter))
-                NotificationCenter.default.post(name: ReaderPagesController.NEW_REFERENCE,
-                                                object: HistoryModel.shared.current())
-                self.navigationController?.popToRootViewController(animated: true)
-            } else {
-                ConcordanceViewController.push(controller: self, section: indexPath.section)
-            }
-        } else {
-            let search = ConcordanceModel.shared.getHistory(row: indexPath.row)
-            self.searchController.setSearchBar(search: search)
-            self.searchController.performLastSearch()
-        }
-    }
-
-    func tableView(_ tableView: UITableView,
-                   editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
-        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
-            return .none
-        } else {
-            return .delete
-        }
-    }
 }
-
-class ConcordanceResultCell : UITableViewCell {
-    let title = UILabel()
-    let verse = UILabel()
-    
-    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
-        super.init(style: style, reuseIdentifier: reuseIdentifier)
-        
-        self.title.font = AppFont.sansSerif(style: .subheadline)
-        self.contentView.addSubview(self.title)
-        
-        self.verse.numberOfLines = 0
-        self.verse.font = AppFont.serif(style: .body)
-        self.contentView.addSubview(self.verse)
-        
-        let inset = self.contentView.frame.width * 0.05
-        
-        self.title.translatesAutoresizingMaskIntoConstraints = false
-        
-        self.title.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: inset).isActive = true
-        self.title.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: inset).isActive = true
-        
-        self.verse.translatesAutoresizingMaskIntoConstraints = false
-        
-        self.verse.topAnchor.constraint(equalTo: self.title.bottomAnchor, constant: inset / 2.0).isActive = true
-        self.verse.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: inset).isActive = true
-        self.verse.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -inset).isActive = true
-        self.verse.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -inset).isActive = true
-    }
-    required init?(coder: NSCoder) {
-        fatalError("ConcordanceResultCell(coder:) is not implemented.")
-    }
-    
-    deinit {
-        print("**** deinit ConcordanceResultCell ******")
-    }
-}

    override func loadView() {
        super.loadView()
        
        self.navigationItem.title = NSLocalizedString("Word Search", comment: "Title of Search page")
        
        // Along with auto layout, these are the keys for enabling variable cell height
        self.tableView.estimatedRowHeight = 88.0

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
            let cell = tableView.dequeueReusableCell(withIdentifier: "concordanceResult", for: indexPath) as! ConcordanceResultCell
            cell.showLastResult(indexPath: indexPath, section: self.section)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "concordanceSearch", for: indexPath)
            cell.contentView.backgroundColor = AppFont.backgroundColor
            cell.textLabel?.textColor = AppFont.textColor
            cell.textLabel?.text = ConcordanceModel.shared.getHistory(row: indexPath.row)
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView,
                   titleForHeaderInSection section: Int) -> String? {
        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
            if self.section == nil {
                //let bookId = ConcordanceModel.shared.resultsByBook[section][0].bookId
                //let bible = HistoryModel.shared.currBible
                //if let name = bible.tableContents?.getBook(bookId: bookId)?.name {
                //    return name
                //} else {
                return "  "
                //}
            }
        }
        return nil
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
            return false
        } else {
            return true
        }
    }
    
    func tableView(_ tableView: UITableView,
                   commit editingStyle: UITableViewCell.EditingStyle,
                   forRowAt indexPath: IndexPath) {
        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_SEARCHES {
            ConcordanceModel.shared.removeHistory(row: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }
    
    //
    // Delegate
    //
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
            let cell = tableView.cellForRow(at: indexPath) as! ConcordanceResultCell
            if cell.verse.text != nil {
                let wordRef = ConcordanceModel.shared.resultsByBook[indexPath.section][indexPath.row]
                HistoryModel.shared.changeReference(bookId: wordRef.bookId, chapter: Int(wordRef.chapter))
                NotificationCenter.default.post(name: ReaderPagesController.NEW_REFERENCE,
                                                object: HistoryModel.shared.current())
                self.navigationController?.popToRootViewController(animated: true)
            } else {
                ConcordanceViewController.push(controller: self, section: indexPath.section)
            }
        } else {
            let search = ConcordanceModel.shared.getHistory(row: indexPath.row)
            self.searchController.setSearchBar(search: search)
            self.searchController.performLastSearch()
        }
    }

    func tableView(_ tableView: UITableView,
                   editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        if self.typeControl.selectedSegmentIndex == ConcordanceViewController.VIEW_LAST_SEARCH {
            return .none
        } else {
            return .delete
        }
    }
}

class ConcordanceResultCell : UITableViewCell {
    let title = UILabel()
    let verse = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.title.font = AppFont.sansSerif(style: .subheadline)
        self.contentView.addSubview(self.title)
        
        self.verse.numberOfLines = 0
        self.verse.font = AppFont.serif(style: .body)
        self.contentView.addSubview(self.verse)
        
        let inset = self.contentView.frame.width * 0.05
        
        self.title.translatesAutoresizingMaskIntoConstraints = false
        
        self.title.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: inset).isActive = true
        self.title.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: inset).isActive = true
        
        self.verse.translatesAutoresizingMaskIntoConstraints = false
        
        self.verse.topAnchor.constraint(equalTo: self.title.bottomAnchor, constant: inset / 2.0).isActive = true
        self.verse.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: inset).isActive = true
        self.verse.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -inset).isActive = true
        self.verse.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -inset).isActive = true
    }
    required init?(coder: NSCoder) {
        fatalError("ConcordanceResultCell(coder:) is not implemented.")
    }
    
    deinit {
        print("**** deinit ConcordanceResultCell ******")
    }
    
    func showLastResult(indexPath: IndexPath, section: Int?) {
        let concordance = ConcordanceModel.shared
        self.contentView.backgroundColor = AppFont.backgroundColor
        
        self.title.textColor = AppFont.textColor
        self.verse.textColor = AppFont.textColor
        
        let bible = HistoryModel.shared.currBible
        
        if indexPath.row < ConcordanceViewController.VIEW_GROUP_SIZE || section != nil {
            let useSection = (section != nil) ? section! : indexPath.section
            let wordRef = concordance.resultsByBook[useSection][indexPath.row]
            let reference = Reference(bibleId: bible.bibleId, bookId: wordRef.bookId,
                                      chapter: Int(wordRef.chapter))
            self.title.text = reference.description(verse: wordRef.verse)
            self.verse.attributedText = self.format(bible: bible, wordRef: wordRef)
            
        } else {
            let wordRefGroup = concordance.resultsByBook[indexPath.section]
            let count = wordRefGroup.count - ConcordanceViewController.VIEW_GROUP_SIZE
            let bookId = wordRefGroup[0].bookId
            let name = bible.tableContents?.getBook(bookId: bookId)?.name
            self.title.text = "\(count) More In \(name ?? bookId)"
            self.verse.text = nil
        }
        self.accessoryType = .disclosureIndicator
    }
    
    private func format(bible: Bible, wordRef: WordRef) -> NSMutableAttributedString? {
