setting and removing highlighting at the right time