Improve UI loop handling.

Only ever have one UI refresh loop running at a time. Create it when needed, destroy it when it's not needed and dispose of it properly on quit.