Debugged and made the script work with sanity checks