New function to calculate the root domain.
The code was taken from stackoverflow.com and should be tested more
thoroughly. I did some limited testing of it and it seems to get the
results we are looking for.