resize buffer function now takes the minimal size needed as a second parameter