Add in extra safeguards to protect against buffer leaks
Fix tests to report buffer leaks and clean up properly