Optimized the core loop

Turned some indirect calls into a flattened switch, and turned a
counter check into a while(true).