All of the functions in here could do with docstrings. I'm getting increasingly worried that if we come back to this code, we won't know what's going on.