I've posted a few of the changes, but you're going to have to accept that this script writes logs to $exedir if it exists - at leats for a while.