Corrections to allow script to run as root - bit of a hack