I find smichr's suggestion much more readable. The rest of the functions below could use docstrings as well.