Improved thread-mgmt to prevent crashes