Moved some functions around: ordered the code into sections