cleanup main function