Improve text selection logic