Make highlighting behave more sanely in all modes, and provide utility functions