Code cleanup and logging improvements.