Reorder a couple of functions and add docstrings.