Clean up array and save to file