     ///
     /// - Parameter realm: a realm instance
     private init(realm: Realm, filteredClasses: [String]?) {
-        self.realm = realm
-        self.filteredClasses = filteredClasses
+        engine = BrowserEngine(realm: realm, filter: filteredClasses)
         super.init(nibName: nil, bundle: nil)
         title = "Realm Browser"
         filterOptions.selectedSegmentIndex = 0

    ///   - indexPath: NSIndexPath
    /// - Returns: a UITableViewCell
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) else {
            fatalError("Could not configure cell.")
        }
        if let cell = cell as? RBSRealmObjectBrowserCell {
            let title = objects[indexPath.row].className
            let count = elementCount(named: title)

            cell.realmBrowserObjectAttributes(title,
                                              detailText: "Objects in Realm = \(count)")
        }
        return cell
    }

    /// TableView DataSource method
    /// Tells the data source to return the number of rows in a given section of a table view.
    ///
    /// - Parameters:
    ///   - tableView: UITableView
    ///   - section: Int
    /// - Returns: number of cells per section
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }

    /// TableView Delegate method
    ///
    /// Asks the delegate for the height to use for a row in a specified location.
    /// A nonnegative floating-point value that specifies the height (in points) that row should be.
    ///
    /// - Parameters:
    ///   - tableView: UITableView
    ///   - indexPath: NSIndexPath
    /// - Returns: height of a single tableView row
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

    /// TableView Delegate method to handle cell selection
    ///
    /// - Parameters:
    ///   - tableView: UITableView
    ///   - indexPath: NSIndexPath
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let object = objects[indexPath.row]
        let results = realm.dynamicObjects(object.className)
        if results.isNonEmpty {
            let viewController = RBSRealmObjectsBrowser(objects: Array(results), realm: realm)
            navigationController?.pushViewController(viewController, animated: true)
        }
    }

    // MARK: - private Methods

    private func elementCount(named name: String) -> Int {
        return realm.dynamicObjects(name).count
    }

    private func configureNavigationBar() {
        navigationItem.titleView = filterOptions
        let bbiDismiss = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: .dismissBrowser)
        let title = RBSSortStyle.ascending.rawValue
        let bbiSort = UIBarButtonItem(title: title, style: .plain, target: self, action: .sortObjects)
        self.navigationItem.rightBarButtonItems = [bbiDismiss, bbiSort]
    }

    private func configureTableView() {
        realmBrowserView.tableView.delegate = self
        realmBrowserView.tableView.dataSource = self
        realmBrowserView.tableView.tableFooterView = UIView()
        realmBrowserView.tableView.register(RBSRealmObjectBrowserCell.self,
                                            forCellReuseIdentifier: cellIdentifier)
        filterOptions.addTarget(self, action: .filterBaseModels, for: .valueChanged)

    }

    private func filterObjects() {
        if let classFilters = filteredClasses {
            objects = objects.filter({ classFilters.contains($0.className) })
        }
    }

    private func fetchObjects() {
        var objectSchema = realm.schema.objectSchema

        if let classFilter = filteredClasses {
            if classFilter.isNonEmpty {
                objectSchema = objectSchema
                    .filter({ classFilter
                        .contains($0.className) })
            }
            if objectSchema.isEmpty {
                objectSchema = realm.schema.objectSchema
            }
        }
        objects = objectSchema
    }
}

// MARK: - Just a more beautiful way of working with selectors
private extension Selector {
    static let dismissBrowser = #selector(RBSRealmBrowser.dismissBrowser)
    static let sortObjects = #selector(RBSRealmBrowser.sortObjects(_:))
    static let filterBaseModels = #selector(RBSRealmBrowser.filterBaseModels(_:))
}

private enum RBSSortStyle: String {
    case ascending = "A-Z"
    case descending = "Z-A"
}

internal final class RBSRealmBrowserView: UIView {
    public var tableView: UITableView
    init() {
        tableView = UITableView(frame: .zero, style: .plain)
        super.init(frame: .zero)
        tableView.backgroundColor = .white
        addSubview(tableView)
        backgroundColor = .white
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func layoutSubviews() {
        super.layoutSubviews()
        let maxWidth: CGFloat = 414.0
        let size = CGSize(width: min(maxWidth, bounds.size.width), height: bounds.size.height)
        var xPos: Double = 0.0
        if Double(size.width) == 414.0 {
            xPos = Double((bounds.size.width - size.width))/2.0
        }
        let origin = (CGPoint(x: xPos, y: 0.0))
        tableView.frame = (CGRect(origin: origin, size: size))
    }
}

        super.viewDidLoad()
        configureNavigationBar()
        configureTableView()
//        fetchObjects()
        BrowserTools.checkForUpdates()
    }

