Changed all the echo() and printf() calls to make log entries instead.
Add 'global' definition for global variables.