Updated Algorithm and Threading