This method is very long, and quite complex. Consider splitting out a few components into private methods. For example, I think there's a `next_word` method hiding in here.