Oh, one more thing....which I should have caught from the start:
Before the dat subroutine you need to put a GOTO Mainloop, otherwise the execution will "fall into" the dat subroutine after the last GOSUB dat you have in there. When it then hits the RETURN there's no matching GOSUB and you'll have a stack underflow and it goes off to la-la-land. I suspect THAT is what you're seeing but the issues with unintentional "array indexing" is still very much relevant!

/Henrik.