Moved around some function to be in proper scope