Preparing to replace main event loop.
Various fixes and improvements.