fix: memory leaks
fix: wrong include
fix: pass by reference
Merge pull request #576 from mmklee/factory_fix

Factory fix