Tidy main function.