Updated functions - I was mistakenly using malloc instead of realloc. While originally I was creating an entirely new buffer and copying the contents over, I thought that that might be a waste of memory, so tried a cleaner approach - then ended up using realloc because of an answer to a stackoverflow question I asked. TO CONFIRM: THIS VERSION WORKS, NOT SURE IF ALL PREVIOUS DO.