Write files and cleanup