refactor main function to be easier to read