Jesus Christ. Note to fucking self: Don't code after 3am. No matter how epic it turns out to be, you will NOT remember how it works the next day, despite copious comments. And God help you if you forget to put comments as you write, assuming you'll understand how you did it tomorrow.
Also note: MAKE YOUR TEST CODE APPARENT AS TEST CODE, SO YOU DON'T TEAR YOUR FUCKING HAIR OUT FOR TWO FUCKING HOURS BECAUSE OF A SINGLE ANTI-LOOP MEASURE YOU PUT IN AS AN AFTERTHOUGHT DURING TESTING. GAH.