Fixed a few crashes and object lifetime issues.

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