Threads fixed, TODO: fix algorithm