Added better thread cleanup support