Remove may remove the last class

Due to not checking the value returned by indexOf, the code removed the last class when it did not find the given class to remove.