logMessage function added (set the logLevel flag to see more or less output).
Some naming changes and simplified some functions.
Other minor things...