Some explanation of what doesn't work would help rather than having to read through all of it to try and determine what isn't working. That being said, right at the top, you are using /inc and /dec incorrectly. Those commands are used to increase or decrease a number. You can't use them to add or remove text from a variable. If you want to do that, look into the various token identifiers ($deltok, $instok, $remtok, $addtok, etc.). That may be the only thing you need to fix, depending on what isn't working.