Move message loop to a better module.  Add debug check.