Working version with additional logging.
Working version, old cruft retained in source.
Cleaned up code.