Final script with complete sanity checks