Rewrote the main function to be more asynchronous.