Do it all in one loop in an attempt to save memory.