Working script, not pretty, but working