fixed a couple of crashes and race conditions