Move functions to top and wrap in conditional