Wow, that’s advanced stuff. A small suggestion: eEvent[j] is always equal to just “j”, you can probably drop eEvent and simplify. Also, make sure your randomtest and quicktest work well with your code. I have used *bug successfully to make sure my code didn’t break.
Finally, for your inspiration, this is a beautiful routine to sort priorities and similar: Seeking simple code to rank a series of stats