This function could be organized better.

Mostly, just adding newlines would be enough. For example, add a newline between lines 96 and 97 to break apart the ideas. Same thing between lines 79 and 80, and maybe also one separating date and time.

When it's broken into smaller chunks, the code is easier to scan and follow