Factored some code from the text view so that we can unit test it more easily.