fixes to the algorithm. cursor preservation now working correctly.