move a large block out of do_monitor() and into a function,