Fixed (seems to) some tricky multithread bugs