With the API_Meta stuff, you'll want to adjust that 4 to be whatever line number that line is on (provided the try/throw/catch all stays on the same line). In this case, it looks like it should be 16. That's how it calculates the actual offset within your script so that you can look up what line is causing an error. (Also, you're missing the Footer portion of the API_Meta setup.) See: <a href="https://app.roll20.net/forum/post/9771314/script-apilogic-gives-if-slash-elseif-slash-else-processing-to-other-scripts/?pageforid=9772430#post-9772430" rel="nofollow">https://app.roll20.net/forum/post/9771314/script-apilogic-gives-if-slash-elseif-slash-else-processing-to-other-scripts/?pageforid=9772430#post-9772430</a> I'd suggest moving everything within the closure of your on('ready',...) event handler. If you don't, you're polluting the global namespace with your own symbols. Things like handleMsg might conflict with other scripts. Roll20 Mod Scripts are all concatenated when the sandbox starts up (hence the need for the API_Meta stuff to figure out where things actually are. If you need to expose functionality so that other scripts can make use of it, switch to the Revealing Module Pattern and return an object with the interface. See: <a href="https://app.roll20.net/forum/post/9787374/token-origin-slash-start-of-turn-location-token/?pageforid=9790892#post-9790892" rel="nofollow">https://app.roll20.net/forum/post/9787374/token-origin-slash-start-of-turn-location-token/?pageforid=9790892#post-9790892</a> Just based on this script, I'm going to guess the reason you're looking to optimize is because you're getting Possible Infinite Loop Detected errors crashing your sandbox? Or possibly just that other scripts are taking a long time to be responsive when your script is active. Your refreshReports() function is called on every change to the turn order and is doing a great deal of work synchronously. Javascript is a single threaded programming language, which means that only one script can be doing something at any given point. If one script is monopolizing the sandbox, it will starve out all the other scripts, making them appear sluggish. If it starves the sandbox too much, it will determine that the script might be in an infinite loop, and will kill the sandbox. The way you avoid both those problems is by building your code in an asynchronous manner. At the fundamental level, that means doing things in smaller chunks and yielding control back to the sandbox periodically so it can reset it's timer and let other scripts do work. The easiest way to do that is to find the places where you iterate on queues of work, and break them up into what I call burndown queues. See: <a href="https://app.roll20.net/forum/permalink/10744870/" rel="nofollow">https://app.roll20.net/forum/permalink/10744870/</a> That's probably enough to get started on further conversation. =D