Tests for spec compliance, fix a couple memory leaks