multithreaded and bugfixed