better threading and bugfixes