It would be nice to have some unit tests around this method now that it's getting more complex.