Shuffle around things, rename functions accurately, and implement tests.

Looks like a big commit but shouldn't change anything
functionality-wise.