             throw StaffError.noteIndexOutOfRange
         }
 
-        // Get second measure if needed (tie starts on last note of measure)
-        var secondMeasure: Measure?
         if noteIndex == firstMeasure.noteCount - 1 {
             let secondNotesHolderIndex: (notesHolderIndex: Int, repeatMeasureIndex: Int?)
             do {

            } catch {
                throw StaffError.noNextNoteToTie
            }
            secondMeasure = try mutableMeasureFromNotesHolderIndex(secondNotesHolderIndex.notesHolderIndex, repeatMeasureIndex: secondNotesHolderIndex.repeatMeasureIndex)
            guard let noteCount = secondMeasure?.noteCount, noteCount > 0 else {
                throw StaffError.noNextNoteToTie
            }
        } else {
            secondMeasure = nil
        }

        // Modify tie
        if secondMeasure != nil {
            try firstMeasure.modifyTie(at: noteIndex, requestedTieState: removeTie ? nil : .begin )
            try secondMeasure?.modifyTie(at: 0, requestedTieState: removeTie ? nil : .end)
        } else {
            if removeTie {
                try firstMeasure.removeTie(at: noteIndex)
            } else {
                try firstMeasure.startTie(at: noteIndex)
            }
        }

        // Set new measures in the staff
        try replaceMeasure(at: measureIndex, with: firstMeasure)
        if let secondMeasure = secondMeasure {
            try replaceMeasure(at: measureIndex + 1, with: secondMeasure)
        }
    }

    internal func notesHolderIndexFromMeasureIndex(_ index: Int) throws -> (notesHolderIndex: Int, repeatMeasureIndex: Int?) {

    case noNextNote
    case notBeginningOfTie
    case repeatedMeasureCannotHaveTie
	case noSameNoteTie
    case measureNotPartOfRepeat
    case repeatedMeasureCannotBeModified
    case cannotInsertRepeatWhereOneAlreadyExists
