fix multithreading behaviour for the workers and fix othe memory leaks

Change-Id: I4fea58dab61649eb9f54136e9b8b49a1a20ba932