Tidy up main function