Add proper reference counting and correct buffer resizing

Signed-off-by: Simon Busch <morphis@gravedo.de>