updated script to be more error resilient and log beter.
increased logging and set permissions