moved buffer allocation outside of loop