Moved this to `utils.py` so that I can use it in other places, which I do in my next patch.