Fixed undefined variable error

Fixed undefined variable error as a result of using global variable
($max_words) inside a function without marking it as global.