fixed memory leaks

Found some condition where there was a memory leak. Fixed