Final Working script with tested code done with device