This magically fixes the error

Despite the fact I'm really not doing anything different other than the
length check.
Also, fixed a long standing bug with the caret going outside the text
entry area.