Further improve thread handling