Arranged functions in a more meaningful order

The functions were somewhat awkwardly organized. Now they are more or less
in the order in which they're used, except for the setters, which are
relatively uninteresting and grouped at the end.