Move main functions to top and helpers to the bottom