Copied in some small changes from the original TextStats class