Hunting memory-leaks... and minor fixes for tests