improved algorithm for line completion.

need to do the same for columns