Moved function definitions around to the right places