Added some more synchronization where needed. I think it's good now, but
of course this stuff is nearly impossible to debug in one night so let
me know if you ever get a java.util.ConcurrentModificationException!