app_monitor: code cleanup, improve task logic