Clean up and reorder some functions.