Fixed possible deadlock / memory leak