Edit the following Python code such that it takes an optional integer value as parameter. This value will be used to limit the maximum number of words in the sentence.
def generateSentence():
    words = ['Hello', 'World', 'He', 'She', 'They', 'Good', 'Bad']
    sentence = ''
    for i in range(len(words)):
        sentence += words[i] + ' '
    return sentence
def generateSentence(limit = None):
    words = ['Hello', 'World', 'He', 'She', 'They', 'Good', 'Bad']
    sentence = ''
    counter = 0
    for i in range(len(words)):
        if not limit or counter < limit:
            sentence += words[i] + ' '
            counter += 1
        else:
            break
    return sentence