rewrited thread loops but code is still horrible ^^