Function to remove more than 2 consecutive repetitions of a string not working