attempt some sensible locking
fix mem leak
fix mem leaks