Simplified it

Completely rewrote the script. it now works ok. await future eptimization!