WIP refactor: changing internal text representation to be line-based.

This will make some of the internals a little more complex, but should
simplify things on the balance.