Reorganize code to group function definitions and improve
readability. Remove replicated code and reduced number of
variables. Add port.close and sys.exit if Minmon is unresponsive.