Tidy up the messy output text building code a little