Move buffer declaration outside the loop