fix lastline/emptyline bugs, and add tests for them.