Write a function in Python that takes in a string and removes all the unnecessary whitespaces.
"This     string  contains   too     much whitespace!"
def remove_whitespaces(string):
    return ' '.join(string.split())
 
print(remove_whitespaces("This     string  contains   too     much whitespace!"))