Clean up of the code and added some extra timestamps so you get a nice log file when you run in as a cronjob