Fix thread blocking bug and other unexpected behaviors