Move reusable code to main.R