See details in script comments