Fix bugs and improve text highlighting