Make sure to free buf before returning