     }
 
     @IBAction func clearMessages(_ sender: Any?) {
+        guard let midiDocument = midiDocument else { return }
+
         midiDocument.clearSavedMessages()
     }
 
     @IBAction func setMaximumMessageCount(_ sender: Any?) {
+        guard let midiDocument = midiDocument else { return }
         guard let control = sender as? NSControl else { fatalError() }
+
         if let number = control.objectValue as? NSNumber {
             midiDocument.maxMessageCount = number.intValue
         }

    }

    @IBAction func setChannelRadioButton(_ sender: Any?) {
        if let matrix = sender as? NSMatrix {
            if matrix.selectedCell()?.tag == 0 {
                midiDocument.showAllChannels()

    // MARK: Filter

    func updateFilterControls() {
        guard let midiDocument = midiDocument else { return }

        let currentMask = midiDocument.filterMask.rawValue

        // Each filter checkbox has a tag with the mask of filters that it is based on.
