refactor main function to support better modularity.