Restructured functions so that they are declared before being called