Refactor.

Renamed variables, moved code, rewrote comments.