fix various compiler warnings
Fix dangerous iterator erasing
Fix 2 possible memory leaks