Completed the reversing script
second approach works now fine