better fix for deathlock and some memory leaks fixes