Fixed a lot of issues with multithreading