cleanup and copy functions.