Fix illegal state exception when closing application, fixes #97