Fixed memory leaks (fixed #241)