better memory utilization with a temporary buffer