I've done paragraph wrapping like this in several langauges. If you don't have to preserve multiple spaces between words you can do something like:
That's a very high level sketch ... there are quite a few little details left out ... finding all the words, spaces between words, two spaces after a period, a single word that is longer than line max and so on. Does it seem to be the right direction?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi