Fixing a multi-thread problem