remove old classes only if the element still exists