Fixed a few crashes and frozen UI issues.

An uninitialised variable, and some object lifetime issues could cause crashes or frozen UI. Fixes issue #42.