cleanup output and main function