Moved functions around to get a code overview