Multithreading doesn't work properly