Reordered function to prevent name not declared error