Cleaned up the code, and set a few log entries to debug.