Fixes several memory leaks. fix #43