fix: memory leaks
fix: wrong include
fix: pass by reference
fix  bug 597
add test to testlist
Merge pull request #576 from mmklee/factory_fix

Factory fix