writing and cleanup