Logging functionality with LOG() and general cleanup.