Fixes memory leaks and some minor refactoring.
Unit tests for the toolkit are consolidated into one test module with a separate test suite for related API functions.