Some fixes and improvements

Most importantly, a memory leakage is fixed.