This method looks broadly useful, so we might want to put it in a utility class higher up the project hierarchy to make it easier to reuse. The effort of moving it might not be worthwhile at the moment though.