Refactored code - amending flow of main.js