Fixed incorrect thread handling